﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Product_Basket : System.Web.UI.UserControl
{
    private int nDefaultLang = Support.GetDefaultLanguage();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.SetMultiLanguage();
            this.ShowMess();
            this.BindDataToGrid(1);
        }
    }
    private void SetMultiLanguage()
    {
        lblKeyWord.Text = MultipleLanguage.LBL_KEYWORD[nDefaultLang - 1];
        lblStatus.Text = MultipleLanguage.LBL_STATUS[nDefaultLang - 1];
        //lblTotalRecord.Text;
    }


    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nDefaultLanguage = nDefaultLang - 1;
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLanguage]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion



    private void BindDataToGrid(int CurrentPageIndex)
    {
        Basket objBasket = new Basket();
        DataTable dtb = new DataTable();
        string sSearch = txtSearch.Text.Trim();
        int nStatus = ConvertData.ConvertToInt(ddlStatus.SelectedValue);
        int nPageCount = 0;
        int nItemView = 0;

        dtb = objBasket.Search(nItemView,sSearch,nStatus , Pager1.CurrentIndex, Config.PAGESIZE, ref nPageCount);
        dtgBasket.DataSource = dtb;
        dtgBasket.DataBind();
        
        int nDefaultLanguage = nDefaultLang;
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage -1];

        Pager1.ItemCount = nPageCount;
        VisiblePaging(nPageCount);
        if (dtb.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;

        txtSearch.Focus();
        txtSearch.Text = ConvertData.RemoveHTMLTag(txtSearch.Text.Trim());

    }



    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }
 

    protected void dtgBasket_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLanguage = nDefaultLang - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblCustomerName = (Label)e.Item.FindControl("lblCustomerName");
           // Label lblEmail = (Label)e.Item.FindControl("lblEmail");
            Label lblAddress = (Label)e.Item.FindControl("lblAddress");
            Label lblCustomerTel = (Label)e.Item.FindControl("lblCustomerTel");
            Label lblDateCreated = (Label)e.Item.FindControl("lblDateCreated");
            Label lnkActive = (Label)e.Item.FindControl("lnkActive");
            Label lnkInactive = (Label)e.Item.FindControl("lnkInactive");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");

            lblCustomerName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CustomerName"));
            lblCustomerTel.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CustomerTel"));
            lblDateCreated.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "DateCreated")));
            lblAddress.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CustomerAddress"));
           //lblEmail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "CustomerEmail"));
            
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (nStatus > 0) // Da xu ly
            {
                lnkInactive.Visible = true;
                lnkActive.Visible = false;
            }
            else
            {
                lnkInactive.Visible = false;
                lnkActive.Visible = true;
            }
          
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BasketID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BasketID"));
        }
    }
    protected void dtgBasket_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = nDefaultLang - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Basket objBasket = new Basket();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objBasket.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindDataToGrid(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BASKET_ADD  + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
           
        }
    }
}
